﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace E1_eMart
{
    public partial class submit_product : System.Web.UI.Page
    {
        protected TaiKhoan taiKhoan = new TaiKhoan();
        protected List<LoaiSanPham> lsplist = new List<LoaiSanPham>();
        protected int loaiTaiKhoan = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] != null)
            {
                taiKhoan = (TaiKhoan)Session["username"];
                loaiTaiKhoan = (int)taiKhoan.MaLoaiTaiKhoan;
            }

            try
            {
                DataClasses1DataContext edata = new DataClasses1DataContext();
                var itemQuery3 = from sp in edata.LoaiSanPhams
                                 select sp;
                lsplist = itemQuery3.ToList();
                ddlLoaiSP.DataSource = lsplist;
                ddlLoaiSP.DataTextField = "TenLoaiSanPham";
                ddlLoaiSP.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }           
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {            
            if (Session["username"] != null)
            {
                try
                {
                    //Add SanPham
                    DataClasses1DataContext edata = new DataClasses1DataContext();
                    taiKhoan = (TaiKhoan)Session["username"];
                    SanPham sp = new SanPham();
                    sp.TenSanPham = txtTenSP.Text.Trim();
                    sp.MaLoaiSanPham = ddlLoaiSP.SelectedIndex + 1;
                    sp.MaTaiKhoan = taiKhoan.MaTaiKhoan;
                    sp.GiaKhoiDiem = txtGiaKhoiDiem.Text.Trim();
                    sp.GiaHienTai = sp.GiaKhoiDiem;                  

                    #region "Start time & End time"
                    //Ngay bat dau                                    
                    int ngay1, thang1, nam1, gio1, phut1, giay1;
                    ngay1 = drdlNgay1.SelectedIndex + 1;
                    thang1 = drdlThang1.SelectedIndex + 1;
                    nam1 = drdlNam1.SelectedIndex + 2010;
                    gio1 = drdlGio1.SelectedIndex;
                    phut1 = drdlPhut1.SelectedIndex;
                    giay1 = drdlGiay1.SelectedIndex;
                    sp.NgayDang = new DateTime(nam1, thang1, ngay1, gio1, phut1, giay1);                                    
                    //Ngay ket thuc                                         
                    int ngay2, thang2, nam2, gio2, phut2, giay2;
                    ngay2 = drdlNgay2.SelectedIndex + 1;
                    thang2 = drdlThang2.SelectedIndex + 1;                        
                    nam2 = drdlNam2.SelectedIndex + 2010;
                    gio2 = drdlGio2.SelectedIndex;
                    phut2 = drdlPhut2.SelectedIndex;
                    giay2 = drdlGiay2.SelectedIndex;
                    sp.NgayHetHan = new DateTime(nam2, thang2, ngay2, gio2, phut2, giay2);                                  
                    #endregion

                    sp.MaTinhTrangSanPham = 1; //Sản phẩm chưa có ai đấu giá
                    sp.MoTaSanPham = txtMoTa.Text.Trim();
                    sp.Videolink = txtLinkVideo.Text.Trim();
                    if (fuImage.HasFile)
                    {
                        String fileName = fuImage.FileName;
                        fuImage.SaveAs(Server.MapPath("~/images/Products/") + fileName);
                        sp.Imglink = "images/Products/" + Path.GetFileName(fileName);
                    }
                    edata.SanPhams.InsertOnSubmit(sp);
                    edata.SubmitChanges();
                    //Show successful message                 
                    string strBuilder = "<script language='javascript'>alert('" + "Thêm thành công" + "')</script>";
                    Response.Write(strBuilder);
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }    

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void Reset()
        {
            txtTenSP.Text = "";
            txtGiaKhoiDiem.Text = "";
            txtMoTa.Text = "";
            txtLinkVideo.Text = "";
        }
    }
}